package com.example.springboot.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.springboot.controller.DTO.LoginDTO;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.service.IAdminService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class AdminService implements IAdminService {

    @Autowired
    AdminMapper adminMapper;

    private static final String DEFAULT_PASS = "00000000";
    private static final String PASS_SALT = "oac";

    @Override
    public List<Admin> list() {
        return adminMapper.list();
    }


    @Override
    public Admin getById(Integer id) {
        return adminMapper.getById(id);
    }

    //修改密码
    @Override
    public void changePassword(PasswordRequest request) {
        request.setNewPass(securePassword(request.getNewPass()));
        int i = adminMapper.updatePassword(request);
        if (i <= 0) {
            throw new ServiceException("密码修改失败");
        }
    }

    //更新
    @Override
    public void update(Admin admin) {
        Date date = new Date();
        admin.setUpdateTime(date);
        adminMapper.update(admin);
    }


    //分页
    @Override
    public PageInfo<Admin> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Admin> admin = adminMapper.listByCondition(baseRequest);
        return new PageInfo<>(admin);
    }

    //新增
    @Override
    public void save(Admin admin) {
        Date date = new Date();
        admin.setUpdateTime(date);
        if (StrUtil.isBlank(admin.getPassword())) {
            admin.setPassword(DEFAULT_PASS);
        }
        admin.setPassword(securePassword(admin.getPassword()));
        adminMapper.save(admin);
    }

    //删除
    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    @Override
    public Integer deleteByIds(List<Integer> ids) {
        return adminMapper.deleteByIds(ids);
    }

    //登录
    @Override
    public LoginDTO login(LoginRequest request) {
        Admin admin = null;
        try {
            admin = adminMapper.getByUsername(request.getUsername());
        } catch (Exception e) {
            log.error("根据用户名{} 查询出错", request.getUsername());
            throw new ServiceException("用户名错误");
        }
        //验证密码是否为空
        if (admin == null) {
            throw new ServiceException("用户名或密码错误");
        }

        //验证密码是否正确
        String securePassword = securePassword(request.getPassword());
        if (!securePassword.equals(admin.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }

        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(admin, loginDTO);

        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword());

        loginDTO.setToken(token);
        return loginDTO;
    }

    //存储、解密密码 设置md5加密 加盐
    private String securePassword(String password) {
        return SecureUtil.md5(password + PASS_SALT);
    }


}



